<?php
class SubjectsController extends AppController {

	var $name = 'Subjects';

	/**
	 *	Todo: View content by subject to be implemented.
	 **/
	function view($name = null) {
		if(!$name) {
			$this->Session->setFlash('Invalid Subject.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('subject', $this->Subject->read(null, $id));
	}

	function admin_index() {
		$this->Subject->recursive = 0;
		$this->set('subjects', $this->paginate());
	}

	function admin_view($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid Subject.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('subject', $this->Subject->read(null, $id));
	}

	function admin_add() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Subject->create();
			if($this->Subject->save($this->data)) {
				$this->Session->setFlash('The Subject has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Subject could not be saved. Please, try again.');
			}
		}
	}

	function admin_edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Subject');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Subject->save($this->data)) {
				$this->Session->setFlash('The Subject saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Subject could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Subject->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Subject');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Subject->del($id)) {
			$this->Session->setFlash('Subject #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}

}
?>